
<?php
if (isset($_POST['editer'])) 
{
	$req = $bdd->prepare('SELECT * FROM creations WHERE id = :id');
	$req->execute(array('id' => $_POST['id']));
	$donnees = $req->fetch();
	?>
	<h1>Editer une ressource</h1>
	<form method="post" action="index.php?page=admin_creations">
		<table border="0">
			<tr><td><label for="titre">Nom</label></td>
			<td><input type="text" name="titre" value="<?php echo stripslashes($donnees['titre']); ?>" maxlength="250" size="50" required></td></tr>
			<tr><td><label for="categorie">Type</label></td>
			<td><select name="categorie" id="categorie">
			<?php
			$req_cat = $bdd->query('SELECT * FROM categories_creations');
			while ($donnees_cat = $req_cat->fetch())
			{
				?>
				<option value="<?php echo $donnees_cat['id']; ?>" <?php if ($donnees_cat['id']==$donnees['categorie']) { echo 'selected'; }  ?>>
				<?php echo stripslashes($donnees_cat['nom']); ?>
				</option>
				<?php
			}
			?>
			</select></td></tr>
			<tr><td colspan="2"><label for="introduction">Introduction</label></td></tr>
			<tr><td colspan="2"><textarea id="introduction" name="introduction" rows="15" cols="80"><?php echo $donnees['introduction']; ?></textarea></td></tr>
			<tr><td colspan="2"><label for="article">Article</label></td></tr>
			<tr><td colspan="2"><textarea id="article" name="article" rows="50" cols="80"><?php echo $donnees['article']; ?></textarea></td></tr>
			<tr><td><label for="date_creation">Date de création</label></td><td><?php echo $donnees['date_creation']; ?> <input type="date" name="date_creation" value="<?php echo $donnees['date_creation']; ?>" maxlength="250" size="50"></td></tr>
			<tr><td colspan="2"><input type="hidden" name="id" value="<?php echo $donnees['id']; ?>" />
			<input type="submit" value="Modifier" name="modifier" class="bouton" /></td></tr>
		</table>
	</form>
	<?php
}
elseif (isset($_POST['modifier'])) 
{
	$req = $bdd->prepare('UPDATE creations SET titre=:titre, categorie=:categorie, introduction=:introduction, article=:article, date_creation=:date_creation WHERE id=:id');
	$req->execute(array(
		'titre' => stripslashes($_POST['titre']),
		'categorie' => $_POST['categorie'],
		'introduction' => $_POST['introduction'],
		'article' => $_POST['article'],
		'date_creation' => $_POST['date_creation'],
		'id' => $_POST['id']
	));
	header('Location: index.php?page=admin_creations'); 
}
elseif (isset($_POST['effacer'])) 
{
	$req = $bdd->prepare('DELETE FROM creations WHERE id=:id');
	$req->execute(array('id' => $_POST['id']));
	header('Location: index.php?page=admin_creations'); 
}
elseif (isset($_POST['ajouter'])) 
{
	$req = $bdd->prepare('INSERT INTO creations (titre, categorie, introduction, article, date_creation) VALUES (:titre, :categorie, :introduction, :article, :date_creation)');
	$req->execute(array(
		'titre' => stripslashes($_POST['titre']),
		'introduction' => $_POST['introduction'],
		'categorie' => $_POST['categorie'],
		'article' => $_POST['article'],
		'date_creation' => $_POST['date_creation']
	));
	header('Location: index.php?page=admin_creations');
}
elseif (isset($_POST['nouveau'])) 
{
	?>
   <h1>Ajouter un article</h1>
   <form method="post" action="index.php?page=admin_creations">
		<table border="0">
			<tr><td><label for="titre">Nom</label></td>
			<td><input type="text" name="titre" value="" maxlength="250" size="80" required></td></tr>
			<tr><td><label for="categorie">Type</label></td><td><select name="categorie" id="categorie">
			<?php
			$req_cat = $bdd->query('SELECT * FROM categories_creations');
			while ($donnees_cat = $req_cat->fetch())
			{
				?><option value="<?php echo $donnees_cat['id']; ?>"><?php echo $donnees_cat['nom']; ?></option><?php
			}
			?>
			</select></td></tr>
			<tr><td colspan="2"><label for="introduction">Introduction</label></td></tr>
			<tr><td colspan="2"><textarea id="introduction" name="introduction" rows="15" cols="100"></textarea></td></tr>
			<tr><td colspan="2"><label for="article">Article</label></td></tr>
			<tr><td colspan="2"><textarea id="article" name="article" rows="50" cols="80"></textarea></td></tr>
			<tr><td><label for="date_creation">Date de création</label></td><td><input type="date" name="date_creation" maxlength="250" size="50"></td></tr>
			<tr><td colspan="2"><input type="submit" value="Ajouter cet article" name="ajouter" class="bouton" /></td></tr>
		</table>
	</form>
	<?php
}
else
{
	?>
	<h1 id="titre">Gestion des creations</h1>
	<form method="post" action="index.php?page=admin_creations">
		<input type="submit" value="Ajouter un nouvel article" name="nouveau" class="bouton" />
	</form>
	<table border="0" width="100%">
	<tr><th><a href="index.php?page=admin_creations&tri=titre">TITRES</a></th><th colspan="2"><a href="index.php?page=admin_creations&tri=categorie">CATEGORIES</a></th></tr>
	<?php
	if (isset($_GET['tri'])) 
	{
		$tri=$_GET['tri'];
		switch ($tri)
		{
			case 'titre':
				$req = $bdd->query('SELECT * FROM creations ORDER BY titre');
			break;
			case 'categorie':
				$req = $bdd->query('SELECT * FROM creations ORDER BY categorie');
			break;
		}
	}
	else
	{
		$req = $bdd->query('SELECT * FROM creations ORDER BY id');
	}
	
	while ($donnees = $req->fetch())
	{
		?>
		<form method="post" action="index.php?page=admin_creations">
			<tr>
			<td width="45%"><?php echo stripslashes($donnees['titre']); ?></td>
			<?php
			$req_cat = $bdd->prepare('SELECT nom FROM categories_creations WHERE id = :id');
			$req_cat->execute(array('id' =>  $donnees['categorie']));
			$donnees_cat = $req_cat->fetch();
			?>
			<td width="25%"><?php echo stripslashes($donnees_cat['nom']); ?></td>
			
			<td width="30%"><input type="hidden" name="id" value="<?php echo $donnees['id']; ?>" />
			<input type="submit" value="Editer" name="editer" class="bouton" />
			<input type="submit" value="Effacer" name="effacer" class="bouton" /></td>
			</tr>
		</form>
		<?php 
	}
	?>
	</table>
	<?php
	$req->closeCursor();
}
